﻿$(function () {
    $("#DateRecorded").datepicker({ dateFormat: 'dd/mm/yy' });
    $("#DateRecordedTo").datepicker({ dateFormat: 'dd/mm/yy' });
});
function ChangeDrugMainType(DrugMainTypeID) {
    //alert($("#_legalDrugPermitType").val());
    var mes = Portal.SODC.IDIS.DrugLegalUpdate.ServerSideGetSubDrugType(DrugMainTypeID).value;
    if (mes.indexOf("ERROR") != -1) {
        alert(mes);
        return;
    }
    //$("#_legalDrugPermitSubType").html("<option>1</option><option>2</option>");
    $("#_legalDrugPermitSubType").html(mes);
}
function AppendDrugPermitItem() {
    if ($("#_legalDrugPermitSubType").val() == "0") {
        alert('Bạn chưa lựa chọn loại ma túy');
        return;
    }
    var mes = Portal.SODC.IDIS.DrugLegalUpdate.ServerSideGetTableForDrugTypeItems($("#_legalDrugPermitSubType option:selected").text(), $("#_legalDrugPermitSubType").val(), $("#_drugLegalKG").val(), $("#_drugLegalL").val(), $("#_drugLegalPercent").val(), $("#_drugTypeItemTemp").html()).value;
    if (mes.indexOf("ERROR") != -1) {
        alert(mes);
        return;
    }
    $("#_drugLegalInfomation").html(mes);
}
function AppendDrugPermitItemEdit() {
    if ($("#_legalDrugPermitSubType").val() == "0") {
        alert('Bạn chưa lựa chọn loại ma túy');
        return;
    }
    var mes = Portal.SODC.IDIS.DrugLegalEdit.ServerSideGetTableForDrugTypeItems($("#_legalDrugPermitSubType option:selected").text(), $("#_legalDrugPermitSubType").val(), $("#_drugLegalKG").val(), $("#_drugLegalL").val(), $("#_drugLegalPercent").val(), $("#_drugTypeItemTemp").html()).value;
    if (mes.indexOf("ERROR") != -1) {
        alert(mes);
        return;
    }
    $("#_drugLegalInfomation").html(mes);
}
function InsertDrugPermit() {
    if ($("#_legalDrugPermitCode").val().trim() == "") { alert('Mã số giấy phép không có dữ liệu!'); $("#_legalDrugPermitCode").focus(); return; }
    _daterecorded = document.getElementById("drpDay_daterecorded").options[document.getElementById("drpDay_daterecorded").selectedIndex].value + '/' + document.getElementById("drpMonth_daterecorded").options[document.getElementById("drpMonth_daterecorded").selectedIndex].value + '/' + document.getElementById("drpYear_daterecorded").options[document.getElementById("drpYear_daterecorded").selectedIndex].value;
    //if ($("#_daterecorded").val().trim() == "") { alert('Ngày cấp phép không có dữ liệu!'); $("#_daterecorded").focus(); return; }
    _dateexpired = document.getElementById("drpDay_dateexpired").options[document.getElementById("drpDay_dateexpired").selectedIndex].value + '/' + document.getElementById("drpMonth_dateexpired").options[document.getElementById("drpMonth_dateexpired").selectedIndex].value + '/' + document.getElementById("drpYear_dateexpired").options[document.getElementById("drpYear_dateexpired").selectedIndex].value;
    //if ($("#_dateexpired").val().trim() == "") { alert('Ngày hết hạn của giấy phép không có dữ liệu!'); $("#_dateexpired").focus(); return; }

    var mes = Portal.SODC.IDIS.DrugLegalUpdate.ServerSideInsertDrugPermited($("#_legalDrugPermitOfMinistry").val(), $("#_legalDrugPermitType").val(), $("#_legalDrugCompany").val(), $("#_legalDrugCompanyPartner").val(), $("#_legalDrugPermitCode").val(), _daterecorded, _dateexpired).value;
    if (mes.indexOf("ERROR") != -1) {
        mes = mes.replace("ERROR:", "").trim();
        alert(mes);
        return;
    }
    alert(mes);
}
function UpdateDrugPermitCustoms() {
    _daterecorded = document.getElementById("drpDay_daterecorded").options[document.getElementById("drpDay_daterecorded").selectedIndex].value + '/' + document.getElementById("drpMonth_daterecorded").options[document.getElementById("drpMonth_daterecorded").selectedIndex].value + '/' + document.getElementById("drpYear_daterecorded").options[document.getElementById("drpYear_daterecorded").selectedIndex].value;
    var mes = Portal.SODC.IDIS.DrugLegalUpdate.ServerSideUpdateDrugPermitedCustoms($("#_permitCodeString").val(), $("#_permitTypeString").val(), $("#_permitByMinistryString").val(), $("#_permitCompanyString").val(), $("#_drugPermitGate").val(), _daterecorded).value;
    if (mes.indexOf("ERROR") != -1) {
        alert(mes);
        return;
    }
    alert(mes);
}
function UpdateDrugPermitUsed() {
    _daterecorded = document.getElementById("drpDay_daterecorded").options[document.getElementById("drpDay_daterecorded").selectedIndex].value + '/' + document.getElementById("drpMonth_daterecorded").options[document.getElementById("drpMonth_daterecorded").selectedIndex].value + '/' + document.getElementById("drpYear_daterecorded").options[document.getElementById("drpYear_daterecorded").selectedIndex].value;
    var mes = Portal.SODC.IDIS.DrugLegalUpdate.ServerSideUpdateDrugPermitedUsed($("#_permitCodeString").val(), $("#_permitByMinistryString").val(), $("#_permitCompanyString").val(), _daterecorded).value;
    if (mes.indexOf("ERROR") != -1) {
        alert(mes);
        return;
    }
    alert(mes);
}
function DeleteDrugPermit(PermitID) {
    var mes = Portal.SODC.IDIS.DrugLegalUpdate.ServerSideDeleteDrugPermit(ItemID).value;
    if (mes.indexOf("ERROR") != -1) {
        alert(mes);
        return;
    }
    alert(mes);
}
function DeleteTempTableItemUpdateFrm(ItemID) {
    var mes = Portal.SODC.IDIS.DrugLegalUpdate.ServerSideDeleteItemTempTable(ItemID).value;
    $("#_drugLegalInfomation").html(mes);
}
/* Region Edit */
function DeleteTempTableItem(ItemID) {
    var mes = Portal.SODC.IDIS.DrugLegalEdit.ServerSideDeleteItemTempTable(ItemID).value;
    $("#_drugLegalInfomation").html(mes);
}
function EditDrugPermit() {
    if ($("#_legalDrugPermitCode").length > 0)
        if ($("#_legalDrugPermitCode").val().trim() == "") { alert('Mã số giấy phép không có dữ liệu!'); $("#_legalDrugPermitCode").focus(); return; }

    //    if ($("#_daterecorded").length > 0)
    //        if ($("#_daterecorded").val().trim() == "") { alert('Ngày cấp phép không có dữ liệu!'); $("#_daterecorded").focus(); return; }
    pdaterecorded = document.getElementById("drpDay_daterecorded").options[document.getElementById("drpDay_daterecorded").selectedIndex].value + '/' + document.getElementById("drpMonth_daterecorded").options[document.getElementById("drpMonth_daterecorded").selectedIndex].value + '/' + document.getElementById("drpYear_daterecorded").options[document.getElementById("drpYear_daterecorded").selectedIndex].value;

    //    if ($("#_dateexpired").length > 0)
    //        if ($("#_dateexpired").val().trim() == "") { alert('Ngày hết hạn của giấy phép không có dữ liệu!'); $("#_dateexpired").focus(); return; }
    pdateexpired = document.getElementById("drpDay_dateexpired").options[document.getElementById("drpDay_dateexpired").selectedIndex].value + '/' + document.getElementById("drpMonth_dateexpired").options[document.getElementById("drpMonth_dateexpired").selectedIndex].value + '/' + document.getElementById("drpYear_dateexpired").options[document.getElementById("drpYear_dateexpired").selectedIndex].value;

    var plevel = ($("#_drugPermitLevel").val() != null) ? $("#_drugPermitLevel").val() : "";
    var pid = ($("#_drugPermitID").val() != null) ? $("#_drugPermitID").val() : "";
    var pcode = ($("#_drugPermitCode").val() != null) ? $("#_drugPermitCode").val() : "";
//    var pdaterecorded = ($("#_daterecorded").val() != null) ? $("#_daterecorded").val() : "";
//    var pdateexpired = ($("#_dateexpired").val() != null) ? $("#_dateexpired").val() : "";

    var pMisnistry = ($("#_legalDrugPermitOfMinistry").val() != null) ? $("#_legalDrugPermitOfMinistry").val() : "";
    var pType = ($("#_legalDrugPermitType").val() != null) ? $("#_legalDrugPermitType").val() : "";
    var pCom = ($("#_legalDrugCompany").val() != null) ? $("#_legalDrugCompany").val() : "";
    var pComPartner = ($("#_legalDrugCompanyPartner").val() != null) ? $("#_legalDrugCompanyPartner").val() : "";
    //alert($("#_legalDrugCompanyPartner").val());
    var pGate = ($("#_legalDrugCompanyPartner").val() != null) ? $("#_legalDrugCompanyPartner").val() : "";

    var mes = Portal.SODC.IDIS.DrugLegalEdit.ServerSideSaveDrugPermitCustoms(plevel, pid, pcode, pdaterecorded, pdateexpired, pMisnistry, pType, pCom, pGate).value;
    if (mes.indexOf("ERROR") != -1) {
        alert(mes);
        return;
    }
    alert(mes);
}

function FilterFunc() {
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    setTimeout("RealFilterFunc('', 0, 20, 10, 0, 'RealFilterFunc')", 10);
}

function RealFilterFunc(belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    PermitCode = document.getElementById("PermitCode").value;
    PermitType = document.getElementById("PermitType").options[document.getElementById("PermitType").selectedIndex].value; //document.getElementById("PermitType").value;
    PerByMinistry = document.getElementById("PerByMinistry").options[document.getElementById("PerByMinistry").selectedIndex].value; //document.getElementById("PerByMinistry").value;
    PermitStatus = document.getElementById("PermitStatus").options[document.getElementById("PermitStatus").selectedIndex].value;
    //DateRecorded = document.getElementById("DateRecorded").value;
    DateRecorded = document.getElementById("drpDayDateRecorded").options[document.getElementById("drpDayDateRecorded").selectedIndex].value + '/' + document.getElementById("drpMonthDateRecorded").options[document.getElementById("drpMonthDateRecorded").selectedIndex].value + '/' + document.getElementById("drpYearDateRecorded").options[document.getElementById("drpYearDateRecorded").selectedIndex].value;
    //DateRecordedTo = document.getElementById("DateRecordedTo").value;
    DateRecordedTo = document.getElementById("drpDayDateRecordedTo").options[document.getElementById("drpDayDateRecordedTo").selectedIndex].value + '/' + document.getElementById("drpMonthDateRecordedTo").options[document.getElementById("drpMonthDateRecordedTo").selectedIndex].value + '/' + document.getElementById("drpYearDateRecordedTo").options[document.getElementById("drpYearDateRecordedTo").selectedIndex].value;

    retValue = Portal.SODC.IDIS.DrugLegalMgr.GetHtmlResult(PermitCode, PermitType, PerByMinistry, PermitStatus, DateRecorded, DateRecordedTo, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript).value;
    if (retValue.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(retValue.replace("ERROR:", ""));
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    document.getElementById("resultcontent").innerHTML = retValue;
}

function ClientDelete(recordid, belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    Msg = Portal.SODC.IDIS.DrugLegalMgr.GetConfirmDeleteMsg().value;
    if (confirm(Msg) == false) return;
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    RetValue = Portal.SODC.IDIS.DrugLegalMgr.ServerSideDelete(recordid).value;
    if (RetValue.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(RetValue.replace("ERROR:", ""));
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    alert(RetValue);
    RealFilterFunc(belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript);
}

function ClientSideExportData() {
    PermitCode = document.getElementById("PermitCode").value;
    PermitType = document.getElementById("PermitType").options[document.getElementById("PermitType").selectedIndex].value; //document.getElementById("PermitType").value;
    PerByMinistry = document.getElementById("PerByMinistry").options[document.getElementById("PerByMinistry").selectedIndex].value; //document.getElementById("PerByMinistry").value;
    PermitStatus = document.getElementById("PermitStatus").options[document.getElementById("PermitStatus").selectedIndex].value;
    //DateRecorded = document.getElementById("DateRecorded").value;
    DateRecorded = document.getElementById("drpDayDateRecorded").options[document.getElementById("drpDayDateRecorded").selectedIndex].value + '/' + document.getElementById("drpMonthDateRecorded").options[document.getElementById("drpMonthDateRecorded").selectedIndex].value + '/' + document.getElementById("drpYearDateRecorded").options[document.getElementById("drpYearDateRecorded").selectedIndex].value;
    //DateRecordedTo = document.getElementById("DateRecordedTo").value;
    DateRecordedTo = document.getElementById("drpDayDateRecordedTo").options[document.getElementById("drpDayDateRecordedTo").selectedIndex].value + '/' + document.getElementById("drpMonthDateRecordedTo").options[document.getElementById("drpMonthDateRecordedTo").selectedIndex].value + '/' + document.getElementById("drpYearDateRecordedTo").options[document.getElementById("drpYearDateRecordedTo").selectedIndex].value;
   
    Ret = Portal.SODC.IDIS.DrugLegalMgr.ServerExportData(PermitCode, PermitType, PerByMinistry, PermitStatus, DateRecorded, DateRecordedTo).value;
    if (Ret.indexOf("ERROR") != -1) {
        alert(Ret.replace("ERROR:", ""));
        return;
    } else {
        window.open(Ret, "_blank");
        //alert(Ret);
    }
}

function ShowClientUpdateUrlForm(PermitID, belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    Url = Portal.SODC.IDIS.DrugLegalMgr.GetShowUpdateForm(PermitID).value;
    if (Url.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(Url.replace("ERROR:", ""));
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    window.open(Url, "_blank");
    RealFilterFunc(belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript);
}

function checkdate(date) {
    var validformat = /^\d{2}\/\d{2}\/\d{4}$/;
    var returnval = false;
    if (!validformat.test(date))
        alert("Nhập sai định dạng ngày tháng.");
    else {
        var dayfield = date.split("/")[0];
        var monthfield = date.split("/")[1];
        var yearfield = date.split("/")[2];
        var dayobj = new Date(yearfield, monthfield - 1, dayfield);
        if ((dayobj.getMonth() + 1 != monthfield) || (dayobj.getDate() != dayfield) || (dayobj.getFullYear() != yearfield))
            alert("Vui lòng nhập lại ngày tháng theo định dạng ngày/tháng/năm.");
        else
            returnval = true;
    }
    return returnval;
}